<?php
/** 留言板
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/12/3 0003
 * Time: 14:31
 */

//导入
include 'mysql.php';
$mysql = new mysql();
$id = $_SESSION["id"];
$name = $_SESSION["username"];
//提交新留言
if (isset($_REQUEST['insert'])) {
    if (!empty($_POST)) {
        $text = $_POST['text'];
        $p_id = $_SESSION['id'];
        $g_id = $_POST['g_id'];
        $time = date('Y-m-d H:i:s');
        $table = 'message';
        $data = array(
            'p_id' => $p_id,
            'g_id' => $g_id,
            'text' => $text,
            'time' => $time
        );
        //"insert into message (p_id,g_id,text,time) VALUES ($p_id,$g_id,'" . $text . "','" . $time . "');";
        $ints = $mysql->insert($data, $table);
        if ($ints == true) {
            alert("发送成功", 0, 'home.php');
        } else {
            alert("发送失败",0,'insert.php');
        }
    }
}
?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
    <script type="text/javascript" charset="utf-8" src="ed/ueditor.config.js"></script>
    <script type="text/javascript" charset="utf-8" src="ed/ueditor.all.min.js"></script>
    <script type="text/javascript" charset="utf-8" src="ed/lang/zh-cn/zh-cn.js"></script>
    <title>留言板</title>
    <style>
        h1{
            text-align: center;
        }
        input{
            width: 50px;
            height: 25px;
        }
        button{
            width: 100px;
            height: 30px;
        }
        #right{
            float: right;
        }
    </style>
</head>
<body>
    <h1>发送留言</h1>
    <form action="insert.php" method="post">
        <script id="editor" name="text" type="text/plain"></script>
        <select name="g_id" id="">
            <?php
            //查询全部用户
            $sql2 = "select * from test_table where dd = 0";
            $user =  $mysql->select_all($sql2);
            foreach($user as $m => $n){
                if($n['id'] != $_SESSION['id']){    ?>
                    <option value="<?php echo $n['id']  ?>">
                        <?php echo $n['username']   ?>
                    </option>
                <?php };?>
            <?php };?>
        </select>
        <input type="submit" value="发送" name="insert" onclick="return on_form()">
    </form>
    <div id="right">
        <button onclick="btn2()">返回</button>
        <button onclick="btn1()">退出登录</button>
    </div>
</body>
</html>
<script type="text/javascript">
    var ue = UE.getEditor('editor');
    function on_form() {
        /*let text = document.getElementById("editor").value;*/
        var text = UE.getEditor('editor').getContent();
        if(text == '' || text == null){
            alert("发送内容不能为空。");
            return false;
        }
    }
    function btn1() {
        var mymessage=confirm("确定要退出登录吗？");
        if(mymessage == true){
            window.location.href="login.php";
            return true;
        }else {
            window.location.href="message.php";
            return false;
        }
    }
    function btn2() {
        window.location.href="home.php";
    }
</script>